package org.ehe.finance.receivable.controller;

import org.ehe.common.core.BaseController;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.finance.receivable.domain.bo.ErpFinanceReceivableBo;
import org.ehe.finance.receivable.domain.vo.ErpFinanceReceivableVo;
import org.ehe.finance.receivable.service.ErpFinanceReceivableService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 应收账款
 * @author :zhangnn
 * @className :ErpFinanceReceivableController
 * @description: TODO
 * @date 2025-08-28 09:30:38
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/erp/finance/receivable")
public class ErpFinanceReceivableController extends BaseController {
    private final ErpFinanceReceivableService receivableService;

    /**
     * 查询应收账款列表
     */
    @SaCheckPermission("erp:receivable:list")
    @GetMapping("/list")
    public TableDataInfo<ErpFinanceReceivableVo> list(ErpFinanceReceivableBo bo, PageQuery pageQuery) {
        return receivableService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出应收账款列表
     */
    @SaCheckPermission("erp:receivable:export")
    @Log(title = "应收账款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpFinanceReceivableBo bo, HttpServletResponse response) {
        List<ErpFinanceReceivableVo> list = receivableService.queryList(bo);
        ExcelUtil.exportExcel(list, "应收账款", ErpFinanceReceivableVo.class, response);
    }

    /**
     * 获取应收账款详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:receivable:query")
    @GetMapping("/{id}")
    public R<ErpFinanceReceivableVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return R.ok(receivableService.queryById(id));
    }

    /**
     * 新增应收账款
     */
    @SaCheckPermission("erp:receivable:add")
    @Log(title = "应收账款", linkType = "finance", businessType = BusinessType.INSERT, idField = "id", nameField = "billNo")
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpFinanceReceivableBo bo) {
        return toAjax(receivableService.insertByBo(bo));
    }

    /**
     * 修改应收账款
     */
    @SaCheckPermission("erp:receivable:edit")
    @Log(title = "应收账款", linkType = "finance", businessType = BusinessType.UPDATE, idField = "id", nameField = "billNo")
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpFinanceReceivableBo bo) {
        return toAjax(receivableService.updateByBo(bo));
    }

    /**
     * 删除应收账款
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:receivable:remove")
    @Log(title = "应收账款", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(receivableService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 检查单据编号唯一性
     */
    @GetMapping("/checkBillNoUnique")
    public boolean checkBillNoUnique(String billNo, Long id) {
        return receivableService.checkBillNoUnique(billNo, id);
    }

    /**
     * 应收账款收款
     */
    @SaCheckPermission("erp:receivable:payment")
    @Log(title = "应收账款收款", businessType = BusinessType.UPDATE)
    @PostMapping("/payment")
    public R<Void> payment(@RequestBody ErpFinanceReceivableBo bo) {
        return toAjax(receivableService.updatePayment(bo.getId(), bo.getPaidAmount()));
    }

    /**
     * 获取客户应收账款汇总
     */
    @SaCheckPermission("erp:receivable:summary")
    @GetMapping("/summary/{customerId}")
    public R<ErpFinanceReceivableVo> getCustomerSummary(@PathVariable Long customerId) {
        return R.ok(receivableService.getCustomerReceivableSummary(customerId));
    }

    /**
     * 获取逾期应收账款列表
     */
    @SaCheckPermission("erp:receivable:overdue")
    @GetMapping("/overdue")
    public TableDataInfo<ErpFinanceReceivableVo> getOverdueList(ErpFinanceReceivableBo bo, PageQuery pageQuery) {
        bo.setIsOverdue(true);
        return receivableService.queryPageList(bo, pageQuery);
    }

    /**
     * 批量收款
     */
    @SaCheckPermission("erp:receivable:batch-payment")
    @Log(title = "批量收款", businessType = BusinessType.UPDATE)
    @PostMapping("/batchPayment")
    public R<Void> batchPayment(@RequestBody List<ErpFinanceReceivableBo> boList) {
        return toAjax(receivableService.batchPayment(boList));
    }
}
